package com.example.mydy;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.adapter.IncomeDetailAdapter;
import com.example.mydy.model.IncomeRecord;
import com.example.mydy.utils.MockDataManager;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;

import java.util.ArrayList;
import java.util.List;

/**
 * 收益详情页面 - 展示收益明细和统计数据
 */
public class IncomeDetailActivity extends AppCompatActivity {
    private static final String TAG = "IncomeDetailActivity";

    private Button btnBack;
    private TextView tvTotalIncome, tvTaskIncome, tvRewardIncome, tvDownlineIncome;
    private PieChart pieChart;
    private RecyclerView rvIncomeDetails;
    private IncomeDetailAdapter adapter;
    private List<IncomeRecord> incomeRecords = new ArrayList<>();

    private MockDataManager mockDataManager;
    private String currentUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income_detail);

        mockDataManager = MockDataManager.getInstance();
        currentUsername = mockDataManager.getCurrentUser().getUsername();

        initViews();
        setupClickListeners();
        loadIncomeData();
        setupPieChart();
    }

    private void initViews() {
        btnBack = findViewById(R.id.btn_back);
        tvTotalIncome = findViewById(R.id.tv_total_income);
        tvTaskIncome = findViewById(R.id.tv_task_income);
        tvRewardIncome = findViewById(R.id.tv_reward_income);
        tvDownlineIncome = findViewById(R.id.tv_downline_income);
        pieChart = findViewById(R.id.pie_chart);
        rvIncomeDetails = findViewById(R.id.rv_income_details);

        // 设置RecyclerView
        rvIncomeDetails.setLayoutManager(new LinearLayoutManager(this));
        adapter = new IncomeDetailAdapter(this, incomeRecords);
        rvIncomeDetails.setAdapter(adapter);
    }

    private void setupClickListeners() {
        btnBack.setOnClickListener(v -> finish());
    }

    private void loadIncomeData() {
        // 获取收益数据
        double taskIncome = mockDataManager.getTodayTaskIncome(currentUsername);
        double rewardIncome = mockDataManager.getTodayRewardIncome(currentUsername);
        double downlineIncome = mockDataManager.getTodayDownlineIncome(currentUsername);
        double totalIncome = taskIncome + rewardIncome + downlineIncome;

        // 更新UI显示
        tvTotalIncome.setText(String.format("¥ %.2f", totalIncome));
        tvTaskIncome.setText(String.format("¥ %.2f", taskIncome));
        tvRewardIncome.setText(String.format("¥ %.2f", rewardIncome));
        tvDownlineIncome.setText(String.format("¥ %.2f", downlineIncome));

        // 加载收益记录列表
        List<IncomeRecord> records = mockDataManager.getUserIncomeRecords(currentUsername);
        incomeRecords.clear();
        incomeRecords.addAll(records);
        adapter.notifyDataSetChanged();
    }

    private void setupPieChart() {
        // 获取数据
        double taskIncome = mockDataManager.getTodayTaskIncome(currentUsername);
        double rewardIncome = mockDataManager.getTodayRewardIncome(currentUsername);
        double downlineIncome = mockDataManager.getTodayDownlineIncome(currentUsername);

        // 创建饼图条目
        List<PieEntry> entries = new ArrayList<>();
        if (taskIncome > 0) {
            entries.add(new PieEntry((float) taskIncome, "任务收益"));
        }
        if (rewardIncome > 0) {
            entries.add(new PieEntry((float) rewardIncome, "悬赏收益"));
        }
        if (downlineIncome > 0) {
            entries.add(new PieEntry((float) downlineIncome, "下级收益"));
        }

        // 设置饼图颜色
        List<Integer> colors = new ArrayList<>();
        colors.add(Color.rgb(76, 175, 80));  // 任务收益 - 绿色
        colors.add(Color.rgb(255, 87, 34));  // 悬赏收益 - 橙色
        colors.add(Color.rgb(33, 150, 243)); // 下级收益 - 蓝色

        // 创建饼图数据集
        PieDataSet dataSet = new PieDataSet(entries, "收益分布");
        dataSet.setColors(colors);
        dataSet.setValueTextSize(14f);
        dataSet.setValueTextColor(Color.WHITE);
        dataSet.setSliceSpace(3f);

        // 创建饼图数据
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter(pieChart));

        // 配置饼图
        pieChart.setData(data);
        pieChart.setUsePercentValues(true);
        pieChart.getDescription().setEnabled(false);
        pieChart.setExtraOffsets(5, 10, 5, 5);
        pieChart.setDragDecelerationFrictionCoef(0.95f);
        pieChart.setCenterText("收益分布");
        pieChart.setCenterTextSize(16f);
        pieChart.setDrawHoleEnabled(true);
        pieChart.setHoleColor(Color.WHITE);
        pieChart.setTransparentCircleColor(Color.WHITE);
        pieChart.setTransparentCircleAlpha(110);
        pieChart.setHoleRadius(58f);
        pieChart.setTransparentCircleRadius(61f);
        pieChart.setDrawCenterText(true);
        pieChart.setRotationAngle(0);
        pieChart.setRotationEnabled(true);
        pieChart.setHighlightPerTapEnabled(true);
        pieChart.getLegend().setEnabled(false);
        pieChart.animateY(1400);
        
        // 刷新图表
        pieChart.invalidate();
    }
} 